package com.ming.tank;

import java.awt.*;
import java.util.Random;

/**
 * @ClassName Tank
 * @Description 坦克类
 * @Author H.M
 * @Date 2021/6/13
 * @Version 1.0
 **/
public class Tank {

    private int x, y;
    private Dir dir = Dir.DOWN;
    private static final int SPEED = 5 ;
    private boolean moving = true;
    public static final int WIDTH = ResourceMgr.goodTankU.getWidth();
    public static final int HEIGHT = ResourceMgr.goodTankU.getHeight();
    public boolean living = true;
    private Random random = new Random();
    private Group group  = Group.BAD;
    Rectangle rect = new Rectangle();

    public Tank(int x, int y, Dir dir,Group group,TankFrame tf) {
        super();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        rect.x = this.x;
        rect.y = this .y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }

    private TankFrame tf = null;

    public void paint(Graphics g) {
        if (!living){
           tf.tanks.remove(this);
        }
        switch (dir){
            case LEFT:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL ,x,y,null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR,x,y,null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU ,x,y,null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD ,x,y,null);
                break;
            default:
                break;
        }
        move();
    }

    private void move() {

        if (!moving) {
            return ;
        }
        switch (dir) {
            case UP:
                y -= SPEED;
                break;
            case LEFT:
                x -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            default:
                break;
        }

        if (this.group == Group.BAD && random.nextInt(100) > 95 ) {
            this.fire();
        }
        //敌方坦克随机移动
        if (this.group == Group.BAD && random.nextInt(100) >95){
            randomDir();
        }

        //边界检测
        boundsCheck();

        //更新rect
        rect.x = this.x;
        rect.y = this.y;

    }

    private void boundsCheck() {
    if (this.x< 2 ) {
        x = 2 ;
    }
    if (this.y < 28 ) {
        y = 28;
    }
    if (this.y > TankFrame.GAME_HEIGHT - Tank.WIDTH - 2) {
        y = TankFrame.GAME_HEIGHT - Tank.WIDTH - 2;
    }
    if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH -2) {
        x = TankFrame.GAME_WIDTH - Tank.WIDTH -2 ;
    }

    }

    private void randomDir() {
        this.dir  = Dir.values()[ random.nextInt(4)];
    }


    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public void fire() {
        int bx = this.x + Tank.WIDTH / 2 - Bullet.WIDTH/2;
        int by = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
        tf.bullets.add(new Bullet(bx,by,this.dir,this.group,this.tf));
        if(this.group == Group.GOOD)  {
            new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
        }
    }
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void die() {
        this.living = false;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }
}
